package com.codename1.samples;

import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.CheckBox;
import com.codename1.ui.Container;
import com.codename1.ui.InterFormContainer;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextField;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.animations.FlipTransition;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.spinner.Picker;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class InterFormContainerSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start_old() {
        if (current != null) {
            current.show();
            return;
        }
        Form f1 = new Form("Form 1", new BorderLayout());
        f1.setTransitionOutAnimator(CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, true, 300));
        Form f2 = new Form("Form 2", new BorderLayout());
        f2.setTransitionOutAnimator(CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, true, 300));

        Form f3 = new Form("Form 3", new BorderLayout());
        Button f3Back = new Button("Back");
        f3Back.addActionListener(e -> {
            f2.showBack();
        });
        f3.add(BorderLayout.CENTER, new SpanLabel("This form doesn't have an interform container"));
        f3.add(BorderLayout.SOUTH, f3Back);
        f1.add(BorderLayout.CENTER, new Label("Hi World"));

        Button sharedButton = new Button("Shared Button");
        sharedButton.addActionListener(e -> {
            if (sharedButton.getComponentForm() == f1) {
                f2.show();
            } else {
                f1.showBack();
            }
        });
        InterFormContainer cnt = new InterFormContainer(sharedButton);
        f1.add(BorderLayout.SOUTH, cnt);

        Button goto3 = new Button("Goto Form 3");
        goto3.addActionListener(e -> {
            f3.show();
        });
        InterFormContainer cnt2 = new InterFormContainer(sharedButton);
        f2.add(BorderLayout.CENTER, BoxLayout.encloseY(new Label("Now on Form 2"), goto3));
        f2.add(BorderLayout.SOUTH, cnt2);

        f1.show();
    }

    public void start() {
        
        
        Button sharedButton = new Button("Shared Button");
        
        InterFormContainer cnt = new InterFormContainer(sharedButton);
        InterFormContainer cnt2 = new InterFormContainer(sharedButton);
        Toolbar.setGlobalToolbar(true);
        Form hi = new Form("Transitions", new BorderLayout());
        Container c = new Container(BoxLayout.y());
        hi.add(BorderLayout.CENTER, c);
        hi.add(BorderLayout.SOUTH, cnt);
        Style bg = hi.getContentPane().getUnselectedStyle();
        bg.setBgTransparency(255);
        bg.setBgColor(0xff0000);
        Button showTransition = new Button("Show");
        Picker pick = new Picker();
        pick.setStrings("Slide", "SlideFade", "Cover", "Uncover", "Fade", "Flip");
        pick.setSelectedString("Slide");
        TextField duration = new TextField("10000", "Duration", 6, TextArea.NUMERIC);
        CheckBox horizontal = CheckBox.createToggle("Horizontal");
        pick.addActionListener((e) -> {
            String s = pick.getSelectedString().toLowerCase();
            horizontal.setEnabled(s.equals("slide") || s.indexOf("cover") > -1);
        });
        horizontal.setSelected(true);
        c.add(showTransition).
                add(pick).
                add(duration).
                add(horizontal);

        Form dest = new Form("Destination", new BorderLayout());
        sharedButton.addActionListener(e -> {
            if (sharedButton.getComponentForm() == hi) {
                dest.show();
            } else {
                hi.showBack();
            }
        });
        dest.add(BorderLayout.SOUTH, cnt2);
        bg = dest.getContentPane().getUnselectedStyle();
        bg.setBgTransparency(255);
        bg.setBgColor(0xff);
        dest.setBackCommand(
                dest.getToolbar().addCommandToLeftBar("Back", null, (e) -> hi.showBack()));

        showTransition.addActionListener((e) -> {
            int h = CommonTransitions.SLIDE_HORIZONTAL;
            if (!horizontal.isSelected()) {
                h = CommonTransitions.SLIDE_VERTICAL;
            }
            switch (pick.getSelectedString()) {
                case "Slide":
                    hi.setTransitionOutAnimator(CommonTransitions.createSlide(h, true, duration.getAsInt(3000)));
                    dest.setTransitionOutAnimator(CommonTransitions.createSlide(h, true, duration.getAsInt(3000)));
                    break;
                case "SlideFade":
                    hi.setTransitionOutAnimator(CommonTransitions.createSlideFadeTitle(true, duration.getAsInt(3000)));
                    dest.setTransitionOutAnimator(CommonTransitions.createSlideFadeTitle(true, duration.getAsInt(3000)));
                    break;
                case "Cover":
                    hi.setTransitionOutAnimator(CommonTransitions.createCover(h, true, duration.getAsInt(3000)));
                    dest.setTransitionOutAnimator(CommonTransitions.createCover(h, true, duration.getAsInt(3000)));
                    break;
                case "Uncover":
                    hi.setTransitionOutAnimator(CommonTransitions.createUncover(h, true, duration.getAsInt(3000)));
                    dest.setTransitionOutAnimator(CommonTransitions.createUncover(h, true, duration.getAsInt(3000)));
                    break;
                case "Fade":
                    hi.setTransitionOutAnimator(CommonTransitions.createFade(duration.getAsInt(3000)));
                    dest.setTransitionOutAnimator(CommonTransitions.createFade(duration.getAsInt(3000)));
                    break;
                case "Flip":
                    hi.setTransitionOutAnimator(new FlipTransition(-1, duration.getAsInt(3000)));
                    dest.setTransitionOutAnimator(new FlipTransition(-1, duration.getAsInt(3000)));
                    break;
            }
            dest.show();
        });
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}
